﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.IO;
using System.Text;

using Joyintech.Common;
using Joyintech.BLL.inpiao365;
using Joyintech.Model.inpiao365;

namespace Joyintech.Admin.inpiao365.general.task
{
    public partial class edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.AddDate.Value = DateTime.Now;

                this.LastExecuteDate.Value = DateTime.Parse("1981-06-11");

                this.TaskId.Text = new Guid().ToString();

                string taskType = DNTRequest.GetQueryString("Task_Type").Trim();

                if (taskType != "")
                {
                    this.ddlTaskType.SelectedValue = taskType;

                    //不同的taskType为list加载不同的内容
                    DataBindList(taskType);
                }

                //判断是否为修改操作
                if (DNTRequest.GetQueryString("Task_Id") != "")
                {
                    DataBindForm();

                    this.btnSaveAs.Visible = true;
                }
                else
                {
                    this.btnSaveAs.Visible = false;
                }
            }
        }

        #region 不同的Task_Type为list加载不同的内容

        #region 主函数

        /// <summary>
        /// 不同的Task_Type为list加载不同的内容
        /// </summary>
        private void DataBindList(string TaskType)
        {
            this.list.Items.Clear();

            switch (TaskType)
            {
                case "Channel":
                    //加载栏目列表
                    DataBindNode(0, "");
                    break;
                case "Content":
                    //加载栏目列表
                    DataBindNode(0, "");
                    break;
                case "File":
                    DataBindFileTemplate();
                    break;
            }
        }

        #endregion

        #region 加载单页模板

        /// <summary>
        /// 加载单页模板
        /// </summary>
        /// <param name="ParentId"></param>
        private void DataBindFileTemplate()
        {
            List<systemTemplate> list = new List<systemTemplate>();

            list = new systemTemplate_BLL().GetModelList("TemplateType='FileTemplate' order by TemplateId");

            if (list != null && list.Count > 0)
            {
                foreach (systemTemplate model in list)
                {
                    ListItem item = new ListItem(model.TemplateName, model.TemplateId.ToString());

                    this.list.Items.Add(item);
                }
            }
        }

        #endregion

        #region 加载栏目列表

        /// <summary>
        /// 加载栏目列表
        /// </summary>
        /// <param name="ParentId"></param>
        private void DataBindNode(int ParentId, string strFront)
        {
            List<systemNode> list = new List<systemNode>();

            list = new systemNode_BLL().GetModelList("ParentID=" + ParentId + " order by NodeOrder");

            if (list != null && list.Count > 0)
            {
                strFront += "-";

                foreach (systemNode nodeModel in list)
                {
                    ListItem item = new ListItem(strFront + nodeModel.NodeName, nodeModel.NodeID.ToString());

                    this.list.Items.Add(item);

                    DataBindNode(nodeModel.NodeID, strFront);
                }
            }
        }

        #endregion

        #endregion

        #region 绑定表单数据

        /// <summary>
        /// 绑定表单数据
        /// </summary>
        private void DataBindForm()
        {
            DataSet ds = new DataSet();

            ds = new createTask_BLL().GetList("TaskId='" + DNTRequest.GetQueryString("Task_Id") + "'");

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                Joyintech.WebControls.HFPanel.BindForm(this.HFPanel1, ds.Tables[0].DefaultView);

                string ServiceParameters = ds.Tables[0].Rows[0]["ServiceParameters"].ToString();

                if (ServiceParameters != "")
                {
                    string[] paras = ServiceParameters.Split(',');

                    foreach (string value in paras)
                    {
                        if (value != "")
                        {
                            this.list.Items.FindByValue(value).Selected = true;
                        }
                    }
                }

                //如果FrequencyType！=“Month、Week、Day、Hour”
                string FrequencyType = ds.Tables[0].Rows[0]["FrequencyType"].ToString();

                if (FrequencyType != "Month" && FrequencyType != "Week" && FrequencyType != "Day" && FrequencyType != "Hour")
                {
                    this.ddlFrequencyType.SelectedValue = "Period";

                    //选中每周期的类型
                    this.ddlPeriodType.SelectedValue = FrequencyType;
                }

                MessageBox.ResponseScript(this, "show('" + this.ddlFrequencyType.SelectedValue + "');"); 
            }
        }

        #endregion

        #region 保存表单数据

        /// <summary>
        /// 保存表单数据到表createTask
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            createTask model = new createTask();

            string strError = string.Empty;

            strError = Joyintech.WebControls.HFPanel.FormCheck(this.HFPanel1, model);

            if (!strError.Equals(""))
            {
                MessageBox.Show(this, strError);
                return;
            }

            Joyintech.WebControls.HFPanel.GetForm(this.HFPanel1, model);

            //获取选中的列表
            foreach (ListItem item in this.list.Items)
            {
                if (item.Selected)
                {
                    model.ServiceParameters += item.Value + ",";
                }
            }

            if (model.FrequencyType == "Period")
            {
                model.FrequencyType = this.ddlPeriodType.SelectedValue;
            }

            //操作结果
            int dataResult = 0;

            if (DNTRequest.GetQueryString("Task_Id") != "")
            {
                model.TaskId = new Guid(DNTRequest.GetQueryString("Task_Id").ToString());
                dataResult = new createTask_BLL().Update(model);
            }
            else
            {
                model.TaskId = Guid.NewGuid();
                dataResult = new createTask_BLL().Add(model);
            }

            if (dataResult >= 1)
            {
                MessageBox.ShowMsgScript(this, "保存成功！", "DG.cancel();ok();");
            }
            else
            {
                MessageBox.Show(this, "保存时发生异常，请重试！");
            }
        }

        #endregion

        #region 保存副本--表单数据

        /// <summary>
        /// 保存表单数据到表createTask
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveAs_Click(object sender, EventArgs e)
        {
            createTask model = new createTask();

            string strError = string.Empty;

            strError = Joyintech.WebControls.HFPanel.FormCheck(this.HFPanel1, model);

            if (!strError.Equals(""))
            {
                MessageBox.Show(this, strError);
                return;
            }

            Joyintech.WebControls.HFPanel.GetForm(this.HFPanel1, model);

            //获取选中的列表
            foreach (ListItem item in this.list.Items)
            {
                if (item.Selected)
                {
                    model.ServiceParameters += item.Value + ",";
                }
            }

            //操作结果
            int dataResult = 0;

            model.TaskId = new Guid();

            dataResult = new createTask_BLL().Add(model);

            if (dataResult >= 1)
            {
                MessageBox.ShowMsgScript(this, "保存成功！", "DG.cancel();ok();");
            }
            else
            {
                MessageBox.Show(this, "保存时发生异常，请重试！");
            }
        }

        #endregion
    }
}